﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using DAL;
using System.Web.Script.Serialization;


namespace 公司CRM客户关系系统.CustomerService
{
    /// <summary>
    /// ServiceAllot 的摘要说明
    /// </summary>
    public class ServiceAllot : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string action=context.Request["action"];
            if(action == "paging")
            {
                int pageIndex = int.Parse(context.Request["pageIndex"]);
                int pageSize = int.Parse(context.Request["pageSize"]);
                PagingList<V_ServiceType> list = BaseDAL.Paging<V_ServiceType>(pageSize, pageIndex, false);
                //返回结果
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(new { total = list.RecordCount, data = list });
                context.Response.Write(json);
            }
            else if (action == "Add")
            {
                CustomServices cs = new CustomServices();
                string CusID = context.Request.Form["ID"];
                string STID = context.Request.Form["STID"];
                string CSTitle = context.Request.Form["CSTitle"];
                string CSDesc = context.Request.Form["CSDesc"];
                string CSName = context.Request.Form["CSName"];

                cs.CusID = CusID;
                cs.STID = Convert.ToInt32(STID);
                cs.CSTitle = CSTitle;
                cs.CSState = Convert.ToInt32(1);
                cs.CSDesc = CSDesc;
                cs.CSCreateID = Convert.ToInt32((context.Session["user"] as V_Users).UserID);
                cs.CSCreateDate = DateTime.Now;
                cs.CSName = CSName;

                int r = BaseDAL.Insert(cs);
                context.Response.Write(r);
            }
            else if (action == "updateCSID")
            {
                //指派人的ID
                int CSDueID = int.Parse(context.Request.Form["CSDueID"]);
                //获取要修改的 CSID
                int CSID = int.Parse(context.Request.Form["CSID"]);
                //接收提交的数据
                CustomServices cs = new CustomServices();
                //CustomServices的到修改ID
                cs.CSID = CSID;
                //修改分派人的ID
                cs.CSDueID = CSDueID;
                //修改服务状态(1=新创建，2=已分配，3=已处理，4=已归档)
                cs.CSState = Convert.ToInt32(2);
                cs.CSDueDate = DateTime.Now;
                //从数据库查出来的值(不需要修改)
                CustomServices ctm = BaseDAL.Find<CustomServices>(CSID);
                cs.STID = ctm.STID;
                cs.CusID = ctm.CusID;
                cs.CSTitle = ctm.CSTitle;
                cs.CSDesc = ctm.CSDesc;
                cs.CSCreateID = ctm.CSCreateID;
                cs.CSCreateDate = ctm.CSCreateDate;
                cs.CSDeal = ctm.CSDeal;
                cs.CSDealDate = ctm.CSDealDate;
                cs.CSResult = ctm.CSResult;
                cs.CSSatisfy = ctm.CSSatisfy;
                cs.CSName = ctm.CSName;
                //修改数据库
                int r = BaseDAL.Update(cs);
                //返回结果
                context.Response.Write(r);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}